package com.bot.rf.util;

import com.sun.org.apache.bcel.internal.generic.FSUB;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @program: report-form-demo
 * @description: 字符串工具类
 * @author: zyy
 * @create: 2021-02-24 14:19
 **/
public class StringUtil {

    //--------------------------- 进制转换 start -----------------------------
    /**
     * 16进制 => ASCII
     *
     * @param s
     * @return
     */
    public static String toAsciiStr(String s) {
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(
                        i * 2, i * 2 + 2), 16));
                if (baKeyword[i] == 0){
                    return "0";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "ASCII");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    /**
     * 获得两个字符之间的字符串
     *
     * @param str
     * @return
     */
    public static String splitData(String str) {
        int endIndex = str.lastIndexOf(".res");
        return str.substring(0, endIndex);
    }

    /**
     * 16进制 => 10进制
     *
     * @param code
     * @return
     */
    public static String toTenStr(String code) {
        return Long.parseLong(code, 16) + "";
    }
    //--------------------------- 进制转换  end  -----------------------------

    /**
     * 按指定大小，分隔集合，将集合按规定个数分为n个部分
     * @param list
     * @param len
     * @return
     */
    public static <T> List<List<T>> splitList(List<T> list, int len) {

        if (list == null || list.isEmpty() || len < 1) {
            return Collections.emptyList();
        }

        List<List<T>> result = new ArrayList<>();

        int size = list.size();
        int count = (size + len - 1) / len;

        for (int i = 0; i < count; i++) {
            List<T> subList = list.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
            result.add(subList);
        }

        return result;
    }

    public static void main(String[] args) {
        String str = "res7171";
        System.out.println(str.substring(3,7));
    }
}
